package com.contract.management.bean.page;

/**
 * 保存查询片段和对应占位符参数
 * @author zhangxl
 *
 */
public class QueryBean {
	
	//列名
	private String name;
	
	//命名参数
	private String namedParam;
	
	//条件 "=", ">", "<", "like"等
	private String cond = "=";
	
	//值，直接加入到ql语句中
	private String value;
	
	//参数，在sql语句中用占位符，加入参数列表中。
	private Object param;
	
	//连接 and, or
	private String conn = "and";
	
	//左括号 "(", "((" 等
	private String leftParen;
	
	//右括号 ")", "))" 等
	private String rightParen;
	
	public QueryBean() {
	}
	
	public QueryBean(String name, String cond) {
		this.name = name;
		this.cond = cond;
	}

	/**
	 * 命名参数和名称相同（将'.'改为'_')
	 * @param name
	 * @param cond
	 * @param param
	 */
	public QueryBean(String name, String cond, Object param) {
		this.name = name;
		this.namedParam = name.replace('.', '_');
		this.cond = cond;
		this.param = param;
	}

	public QueryBean(String name, String cond, Object param, String namedParam) {
		this.name = name;
		this.namedParam = namedParam;
		this.cond = cond;
		this.param = param;
	}
	
	public String getName() {
		return name;
	}
	public QueryBean setName(String name) {
		this.name = name;
		return this;
	}
	public String getCond() {
		return cond;
	}
	public QueryBean setCond(String cond) {
		this.cond = cond;
		return this;
	}
	public String getValue() {
		return value;
	}
	public QueryBean setValue(String value) {
		this.value = value;
		return this;
	}
	public Object getParam() {
		return param;
	}
	public QueryBean setParam(Object param) {
		this.param = param;
		return this;
	}
	
	public String getConn() {
		return conn;
	}

	public QueryBean setConn(String conn) {
		this.conn = conn;
		return this;
	}

	public String getLeftParen() {
		return leftParen;
	}

	public QueryBean setLeftParen(String leftParen) {
		this.leftParen = leftParen;
		return this;
	}

	public String getRightParen() {
		return rightParen;
	}

	public QueryBean setRightParen(String rightParen) {
		this.rightParen = rightParen;
		return this;
	}
	
	public String getNamedParam() {
		return namedParam;
	}

	public QueryBean setNamedParam(String namedParam) {
		this.namedParam = namedParam;
		return this;
	}
	
	/**
	 * 生成查询条件子句，不包括 conn
	 * 
	 * @param objName 别名
	 * @return
	 */
	public String getClause(String objName) {
		StringBuilder sb = new StringBuilder();
		if(this.leftParen != null) sb.append(this.leftParen);
		if(objName != null) sb.append(objName).append(".");
		sb.append(this.name);
		sb.append(" ").append(this.cond);
		if(this.value != null) {
			sb.append(" ").append(this.value);
		} else {
			if("in".equals(this.cond)) {
				sb.append(" (:").append(this.namedParam).append(")");
			} else {
				sb.append(" :").append(this.namedParam);
			}
		}
		if(this.rightParen != null) sb.append(this.rightParen);
		return sb.toString();
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(conn).append(" ");
		if(leftParen != null ) sb.append(leftParen);
		sb.append(name).append(" ").append(cond).append(" :").append(namedParam).append("(");
		if(value != null) sb.append(value);
		if(param != null) sb.append(param);
		sb.append(")");
		if(rightParen != null) sb.append(rightParen);
		return sb.toString();
	}
}
